package shiyl.scaffold.module.demo.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static java.lang.String.format;

@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @NacosInjected
    private NamingService namingService;

    @GetMapping("/nacos/{serviceName}")
    public List<Instance> get(@PathVariable String serviceName) throws NacosException {
        log.info("DemoController.get");
        return namingService.getAllInstances(serviceName);
    }

    @GetMapping("/hello")
    public String hello( @RequestParam  String who){
        log.info("DemoController.hello");
        return "hello " + who ;
    }

    @NacosValue("${demo.who}")
    private String who ;
    @Value("${demo.name}")
    private String name ;
    @Value("${demo.age}")
    private String age ;


    @GetMapping("/nacos/config")
    public String nacosConfig() throws NacosException {

        return format("用@NacosValue获取不到who {%s} , 用@Value获取到 name %s, age %s",who,name,age);
    }

}
